<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;
use think\Db;

/**
 * 好评晒图管理
 *
 * @icon fa fa-circle-o
 */
class Praise extends Backend
{
    
    /**
     * Praise模型对象
     * @var \app\common\model\Praise
     */
    protected $model = null;

    protected $order_model = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\order\Praise;
        $this->order_model = new \app\admin\model\shop\Order;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['order'=>function($q){
                    $q->with(['shop','admin','business','user']);
                }])
                ->field('*,order_id as id')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     *
     * 改变状态
     *
     * 审核通过 驳回按钮
     */

    public function update_status(){

            if ($this->request->isPost()) {
                $params = $this->request->post();
                if ($params)
                {
                    $praise = Db::name("praise")->where("praise_id",$params["praise_id"])->find();//好评信息

                    if($praise["auth_status"]==1){
                        renderJson("已审核",0);
                    }

                    $order = Db::name("shop_order")->where("order_id",$praise["order_id"])->find();

                    if($order["status"]!=11){
                        renderJson("不在审核状态",0);
                    }
                    Db::startTrans();

                    switch ($params["type"]) {

                        case 1:

                            if($order["order_state"]==1){
                                renderJson("撤销状态，不可审核",0);
                            }

                            $res = $this->order_model->examReject($order["order_id"], 10, 1);

                            if ($res) {
                            $this->model->save(["auth_status" => 1, "update_time" => time()], ["praise_id" => $params["praise_id"]]);
                            Db::commit();

                            renderJson("审核成功", 1);

                            }

                            break;

                        case 2:

                            if($order["order_state"]==1){
                                renderJson("撤销状态，不可审核",0);
                            }


                            $res = $this->order_model->examReject($order["order_id"], 10, 2);

                            if ($res) {
                                Db::name("praise")->where(["praise_id" => $params["praise_id"]])->delete();
                                Db::commit();

                                renderJson("驳回成功", 1);
                            }
                            break;


                        case 3:

                            //可撤销组合状态
                            $cancle_arr = [1,2,3,4,5,6,7,8,9,10,11,12];
                            if(!in_array($order["status"],$cancle_arr)){
                                renderJson("该状态不可以撤销",0);
                            }
                            //
                            $res = $this->model->save(["order_state"=>1,"update_time"=>time()],["order_id"=>$order["order_id"]]);

                            Db::commit();

                            if($res){

                                renderJson("撤销成功", 1);
                            }

                            break;

                        case 4:

                            //订单状态为1的时候才可以回收
                            if($order["order_state"]!=1){
                                renderJson("该状态不可以回收",0);
                            }

                            $res = $this->model->save(["order_state"=>2,"update_time"=>time()],["order_id"=>$order["order_id"]]);

                            Db::commit();

                            if($res){

                                renderJson("回收成功", 1);
                            }

                            break;
                    }
                }
            }
    }








    

}
